const service = require('../service/file_service')
const userService = require('../service/user_service')
const {APP_HOST, APP_PORT} = require('../app/config')

class fileController{
  async saveAvatarInfo(ctx,next){
    // 获取图片相关数据
    const {mimetype, filename, size} = ctx.req.file
    const {id} = ctx.user
    // 将数据保存到数据库
    const result = await service.saveAvatar(filename, mimetype, size, id)
    // 将图片地址保存到user表
    const avatarUrl = `${APP_HOST}:${APP_PORT}/users/${id}/avatar`
    await userService.updateAvatarUrl(id,avatarUrl)

    ctx.body = '上传图片成功'
  }
  async savePictureInfo(ctx,next){
    // 获取图像信息
    const files = ctx.req.files
    const {id} = ctx.user
    const {momentId} = ctx.query

    // 将所有的信息保存到数据库
    for(let file of files){
      const {filename, mimetype, size} = file
      await service.createFile(filename,mimetype,size,id,momentId)
    }
    ctx.body = '动态图片上传成功'
  }
}

module.exports = new fileController()